package com.school.service.impl;

import com.school.common.ServiceResultEnum;
import com.school.entity.Goods;
import com.school.entity.GoodsCategory;
import com.school.mapper.GoodsCategoryMapper;
import com.school.mapper.GoodsMapper;
import com.school.service.GoodsService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public PageResult getGoodsPage(PageQueryUtil pageQueryUtil) {
        List<Goods> list = goodsMapper.findGoodsList(pageQueryUtil);
        int totalCount = goodsMapper.getTotalGoodsCount(pageQueryUtil);
        PageResult pageResult = new PageResult(list, totalCount, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }
    @Override
    public String saveGoods(Goods goods) {
        if (goodsCategoryMapper.selectById(goods.getGoodsCategoryId()) == null) {
            return ServiceResultEnum.GOODS_CATEGORY_ERROR.getResult();
        }
        Goods temp = goodsMapper.selectByNameCategoryId(goods.getGoodsName() ,goods.getGoodsCategoryId());
        if (temp != null) {
            return ServiceResultEnum.SAME_GOODS_EXIST.getResult();
        }
        if (goodsMapper.insertSelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

}
